package com.cheng.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheng.dao.system.MenuDao;
import com.cheng.domain.system.Menu;
import com.cheng.domain.system.User;
import com.cheng.service.system.MenuService;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;

	@Override
	public List<Menu> findMenu() {
		return menuDao.findByParentMenuIsNull();
	}

	@Override
	public void saveMenu(Menu model) {
		if (model.getParentMenu() != null && model.getParentMenu().getId() == 0 ) {
			model.setParentMenu(null);
		}
		menuDao.save(model);
	}


	@Override
	public List<Menu> findMenuByUser(User user) {
		if ("admin".equals(user.getUsername())) {
			return menuDao.findAll();
		}
		return menuDao.findByUser(user.getId());
	}

}
